package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.QrCodeInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.QrCodeInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.QrCodeInfo;
import cc.rengu.igas.bomp.common.enums.QrBindStatusEnum;
import cc.rengu.igas.bomp.common.enums.QrStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.MchntQrCodeManageRequest;
import cc.rengu.igas.bomp.facade.response.MchntQrCodeManageResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 商户收款聚合码管理服务
 * Created by 王成 on 2018/10/18.
 */
public class MchntQrCodeManageService extends ManageService {

    /**
     * 操作类型：停用
     */
    private static final String OPT_TYPE_STOP = "01";

    /**
     * 操作类型：启用
     */
    private static final String OPT_TYPE_START = "02";


    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntQrCodeManageRequest mchntQrCodeManageRequest = new MchntQrCodeManageRequest();
        ConvertUtil.convertOutput(mchntQrCodeManageRequest);
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(mchntQrCodeManageRequest.getMchntNo())
                || StringUtil.isEmptyOrNull(mchntQrCodeManageRequest.getMchntQrCode())
                || StringUtil.isEmptyOrNull(mchntQrCodeManageRequest.getOprType())) {
            rglog.error("商户号,商户收款聚合码,操作类型不能为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (!(OPT_TYPE_STOP.equals(mchntQrCodeManageRequest.getOprType())
                || OPT_TYPE_START.equals(mchntQrCodeManageRequest.getOprType()))) {
            rglog.error("操作类型检查失败！操作类型只能为01-暂停，02-启动！传入操作类型：{}", mchntQrCodeManageRequest.getOprType());
            throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
        }

        return mchntQrCodeManageRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntQrCodeManageRequest mchntQrCodeManageRequest = (MchntQrCodeManageRequest) request;
        String instId = mchntQrCodeManageRequest.getHeader().getInstId();

        /* 查询商户收款聚合码信息 */
        QrCodeInfoMapper qrCodeInfoMapper = new QrCodeInfoMapperImpl();
        QrCodeInfo qrCodeInfo = qrCodeInfoMapper.selectQrCodeInfoByQrCode(instId, mchntQrCodeManageRequest.getMchntQrCode());
        if (null == qrCodeInfo) {
            rglog.error("机构<{}>商户收款聚合码<{}>不存在!", instId, mchntQrCodeManageRequest.getMchntQrCode());
            throw new BizException(RespCodeEnum.GET_QRCODE_INFO_ERROR.getRespCode(), RespCodeEnum.GET_QRCODE_INFO_ERROR.getRespDesc());
        }

        if (QrBindStatusEnum.UNBIND.getBindStatus().equals(qrCodeInfo.getBindStatus()) ||
                !mchntQrCodeManageRequest.getMchntNo().equals(qrCodeInfo.getMchntNo())) {
            rglog.error("商户收款聚合码还未绑定商户,或者绑定商户<{}>与操作商户<{}>不一致!",
                    qrCodeInfo.getMchntNo(), mchntQrCodeManageRequest.getMchntNo());
            throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
        }

        if (OPT_TYPE_STOP.equals(mchntQrCodeManageRequest.getOprType())) {
            /* 停用 */
            if (!QrStatusEnum.NORMAL.getStatus().equals(qrCodeInfo.getQrStatus())) {
                rglog.error("商户收款聚合码状态为：{}，不允许进行停用操作!",
                        qrCodeInfo.getQrStatus());
                throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
            }

            qrCodeInfo.setQrStatus(QrStatusEnum.STOP.getStatus());
            qrCodeInfo.setLastOprId(mchntQrCodeManageRequest.getUserId());
            qrCodeInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        } else if (OPT_TYPE_START.equals(mchntQrCodeManageRequest.getOprType())) {
            /* 启用 */
            if (!QrStatusEnum.STOP.getStatus().equals(qrCodeInfo.getQrStatus())) {
                rglog.error("商户收款聚合码状态为：{}，不允许进行启用操作!",
                        qrCodeInfo.getQrStatus());
                throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
            }

            qrCodeInfo.setQrStatus(QrStatusEnum.NORMAL.getStatus());
            qrCodeInfo.setLastOprId(mchntQrCodeManageRequest.getUserId());
            qrCodeInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        } else {
            rglog.error("不支持的操作类型<{}>", mchntQrCodeManageRequest.getOprType());
            throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
        }
        /* 更新二维码维定信息 */
        int iReturnCode = qrCodeInfoMapper.updateQrCodeInfoById(qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("更新机构<{}>商户收款码<{}>状态失败!", instId, mchntQrCodeManageRequest.getMchntQrCode());
            throw new BizException(RespCodeEnum.UPDATE_QRCODE_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_QRCODE_INFO_ERROR.getRespDesc());
        }
        /* 组装应答信息 */
        MchntQrCodeManageResponse mchntQrCodeManageResponse = new MchntQrCodeManageResponse();
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntQrCodeManageResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntQrCodeManageResponse mchntQrCodeManageResponse = (MchntQrCodeManageResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntQrCodeManageResponse);
    }
}
